package com.ruoyi.business.domain.dto.admin.product;

import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.business.domain.product.TbProduct;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;

/**
 * 商品主表Excel导出DTO
 */
@Data
@ExcelIgnoreUnannotated
@ApiModel(value = "TbProductExportDTO", description = "商品主表Excel导出数据传输对象")
public class TbProductExportDTO {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 商品主键ID
     */
    @ExcelProperty(value = "商品ID")
    @ApiModelProperty(value = "主键id")
    private Long id;

    /**
     * SPU编码
     */
    @ExcelProperty(value = "SPU编码")
    @ApiModelProperty(value = "SPU编码（商品统一编码）")
    private String spuCode;

    /**
     * 商品标题
     */
    @ExcelProperty(value = "商品标题")
    @ApiModelProperty(value = "商品标题")
    private String title;

    /**
     * 商品副标题
     */
    @ExcelProperty(value = "商品副标题")
    @ApiModelProperty(value = "商品副标题（补充说明）")
    private String subtitle;

    /**
     * 分类ID
     */
    @ExcelProperty(value = "分类ID")
    @ApiModelProperty(value = "商品所属分类ID")
    private Long categoryId;

    /**
     * 主图数组字符串
     */
    @ExcelProperty(value = "主图URL列表")
    @ApiModelProperty(value = "主图数组（逗号分隔的URL）")
    private String mainImagesStr;

    /**
     * 详情图数组字符串
     */
    @ExcelProperty(value = "详情图URL列表")
    @ApiModelProperty(value = "详情图数组（JSON字符串格式）")
    private String detailImagesStr;

    /**
     * 主图视频
     */
    @ExcelProperty(value = "主图视频URL")
    @ApiModelProperty(value = "主图视频URL")
    private String videoUrl;

    /**
     * 是否推荐
     */
    @ExcelProperty(value = "是否推荐")
    @ApiModelProperty(value = "是否推荐（0-否，1-是）")
    private Integer isRecommend;

    /**
     * 是否热销
     */
    @ExcelProperty(value = "是否热销")
    @ApiModelProperty(value = "是否热销（0-否，1-是）")
    private Integer isHot;

    /**
     * 是否新品
     */
    @ExcelProperty(value = "是否新品")
    @ApiModelProperty(value = "是否新品（0-否，1-是）")
    private Integer isNew;

    /**
     * 最低价
     */
    @ExcelProperty(value = "最低价")
    @ApiModelProperty(value = "商品最低价")
    private String minPrice;

    /**
     * 最高价
     */
    @ExcelProperty(value = "最高价")
    @ApiModelProperty(value = "商品最高价")
    private String maxPrice;

    /**
     * 总库存
     */
    @ExcelProperty(value = "总库存")
    @ApiModelProperty(value = "商品总库存数量")
    private Long totalStock;

    /**
     * 总销量
     */
    @ExcelProperty(value = "总销量")
    @ApiModelProperty(value = "商品总销量（真实销量）")
    private Long saleCount;

    /**
     * 虚拟销量
     */
    @ExcelProperty(value = "虚拟销量")
    @ApiModelProperty(value = "商品虚拟销量")
    private Long virtualSaleCount;

    /**
     * 状态
     */
    @ExcelProperty(value = "状态")
    @ApiModelProperty(value = "商品状态（0-下架，1-上架，2-审核中）")
    private Integer status;

    /**
     * 排序值
     */
    @ExcelProperty(value = "排序值")
    @ApiModelProperty(value = "排序值（数字越大越靠前）")
    private Long sortOrder;

    /**
     * 产品参数
     */
    @ExcelProperty(value = "产品参数")
    @ApiModelProperty(value = "产品参数（JSON格式，键值对）")
    private String specificationsStr;

    /**
     * 搜索关键词
     */
    @ExcelProperty(value = "搜索关键词")
    @ApiModelProperty(value = "搜索关键词（逗号分隔）")
    private String searchKeywords;

    /**
     * 创建时间
     */
    @ExcelProperty(value = "创建时间")
    @ApiModelProperty(value = "创建时间")
    private Date createTime;

    /**
     * 更新时间
     */
    @ExcelProperty(value = "更新时间")
    @ApiModelProperty(value = "更新时间")
    private Date updateTime;

    /**
     * 创建人
     */
    @ExcelProperty(value = "创建人")
    @ApiModelProperty(value = "创建人")
    private String createBy;

    /**
     * 更新人
     */
    @ExcelProperty(value = "更新人")
    @ApiModelProperty(value = "更新人")
    private String updateBy;

    /**
     * 删除状态
     */
    @ExcelProperty(value = "删除状态")
    @ApiModelProperty(value = "商品状态（0-未删除，1-已删除）")
    private Integer isDelete;

    /**
     * 将TbProduct实体转换为导出DTO
     *
     * @param product 商品实体
     * @return 导出DTO
     */
    public static TbProductExportDTO convertFrom(TbProduct product) {
        TbProductExportDTO dto = new TbProductExportDTO();

        dto.setId(product.getId());
        dto.setSpuCode(product.getSpuCode());
        dto.setTitle(product.getTitle());
        dto.setSubtitle(product.getSubtitle());
        dto.setCategoryId(product.getCategoryId());

        // 处理主图数组
        if (product.getMainImages() != null) {
            dto.setMainImagesStr(String.join(",", product.getMainImages()));
        }

        // 处理详情图数组
        if (product.getDetailImages() != null) {
            try {
                dto.setDetailImagesStr(objectMapper.writeValueAsString(product.getDetailImages()));
            } catch (JsonProcessingException e) {
                dto.setDetailImagesStr("");
            }
        }

        dto.setVideoUrl(product.getVideoUrl());
        dto.setIsRecommend(product.getIsRecommend());
        dto.setIsHot(product.getIsHot());
        dto.setIsNew(product.getIsNew());
        dto.setMinPrice(product.getMinPrice());
        dto.setMaxPrice(product.getMaxPrice());
        dto.setTotalStock(product.getTotalStock());
        dto.setSaleCount(product.getSaleCount());
        dto.setVirtualSaleCount(product.getVirtualSaleCount());

        // 处理产品参数
        if (product.getSpecifications() != null) {
            try {
                dto.setSpecificationsStr(objectMapper.writeValueAsString(product.getSpecifications()));
            } catch (Exception e) {
                dto.setSpecificationsStr(product.getSpecifications().toString());
            }
        }

        dto.setSearchKeywords(product.getSearchKeywords());
        dto.setCreateTime(product.getCreateTime());
        dto.setUpdateTime(product.getUpdateTime());
        dto.setCreateBy(product.getCreateBy());
        dto.setUpdateBy(product.getUpdateBy());
        dto.setIsDelete(product.getIsDelete());

        return dto;
    }
}
